package intellij.haskell.testIntegration;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;

/**
 * Binding of the "Create test" dialog. Responsible for fields declaration, validation and exiting the dialog (either by clicking OK, Cancel or closing the window).
 *
 * @implNote This is a Java file because Intellij integration of dialogs / forms only supports Java, not Scala.
 */
public class CreateHaskellTestDialog extends DialogWrapper {
    private JPanel mainPanel;
    private JTextField moduleName;

    public CreateHaskellTestDialog(@Nullable Project project) {
        // Basic configuration required by DialogWrapper
        super(project);
        init();
        setTitle("Create New Test Module");

        // Fields configuration
        moduleName.getDocument().addDocumentListener(new MandatoryTextFieldListener());
    }

    /**
     * @return A unique key so that dialog window resizing is remembered for future tests dialog creation
     */
    @Nullable
    @Override
    protected String getDimensionServiceKey() {
        return "#haskell.test.module.creation.dialog";
    }

    /**
     * Needed by Intellij to bind this class to the form.
     */
    @Override
    protected JComponent createCenterPanel() {
        return mainPanel;
    }

    private boolean isValid() {
        return !StringUtil.isEmptyOrSpaces(moduleName.getText());
    }

    public String getModuleName() {
        return moduleName.getText().trim();
    }

    public void setModuleName(String text) {
        moduleName.setText(text);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayoutManager(1, 2, new Insets(0, 0, 0, 0), -1, -1));
        moduleName = new JTextField();
        mainPanel.add(moduleName, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        final JLabel label1 = new JLabel();
        label1.setText("Module name");
        label1.setToolTipText("Name of the file (and module) that will be created");
        mainPanel.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return mainPanel;
    }

    private class MandatoryTextFieldListener implements DocumentListener {
        @Override
        public void insertUpdate(DocumentEvent documentEvent) {
            getOKAction().setEnabled(isValid());
        }

        @Override
        public void removeUpdate(DocumentEvent documentEvent) {
            getOKAction().setEnabled(isValid());
        }

        @Override
        public void changedUpdate(DocumentEvent documentEvent) {
            getOKAction().setEnabled(isValid());
        }
    }
}
